package trying;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamTest {
    private String name;
    private int age;
    private int code;

    public StreamTest(String name, int age, int code) {
        this.name = name;
        this.age = age;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public static void main(String[] args) {
        StreamTest Stream = new StreamTest("小名",20,115);
        StreamTest Stream1 = new StreamTest("小名",24,115);
        StreamTest Stream2 = new StreamTest("小名",20,115);

        List<StreamTest> StreamList = Arrays.asList(Stream, Stream1, Stream2);

        //按照年龄分组
        Map<Integer, List<StreamTest>> ageMap = StreamList.stream().collect(Collectors.groupingBy(StreamTest::getAge));
        System.out.println("ageMap = " + ageMap);

        //按照年龄分组，并求出个数
        Map<Integer, Long> ageMap2 = StreamList.stream().collect(Collectors.groupingBy(StreamTest::getAge, Collectors.counting()));
        System.out.println("ageMap2 = " + ageMap2);



    }
}